package com.ruoyi.project.modules.parameter.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.parameter.domain.Params;
import com.ruoyi.project.modules.parameter.service.IParamsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参数数据集Controller
 *
 * @author smallrain
 * @date 2021-10-30
 */
@Controller
@RequestMapping("/parameters/parameter")
public class ParametersController extends BaseController {
    private String prefix = "parameter/parameter";

    @Autowired
    private IParamsService paramsService;

    @Autowired
    private IVirtualUsageService virtualUsageService;

    @Autowired
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;

    @RequiresPermissions("parameter:parameter:view")
    @GetMapping()
    public String parameter() {
        return prefix + "/parameter";
    }

    /**
     * 查询参数数据集列表
     */
    //@RequiresPermissions("parameter:parameter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Params params) {
        List<Long> ids = new ArrayList<>();
        boolean fixedFlag = false;
        if (params.getInterfaceId() != null) {
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(params.getInterfaceId());
            virtualUsage.setIsMaster("Y");
            List<VirtualUsage> virtualUsages = virtualUsageService.selectVirtualUsageList(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                fixedFlag = true;
                for (VirtualUsage usage : virtualUsages) {
                    InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
                    interfaceParamsFieldRelation.setType("3");
                    interfaceParamsFieldRelation.setInterfaceId(usage.getId());
                    interfaceParamsFieldRelation.setObjectType("2");
                    List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList = interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
                    List<Long> idList = interfaceParamsFieldRelationList.stream().map(o -> o.getObjectId()).collect(Collectors.toList());
                    ids.addAll(idList);
                }
            }
        }
        startPage();
        params.setIsInParam(null);
        params.setIds(ids);
        if (fixedFlag && CollectionUtil.isEmpty(ids)) {
            return getDataTable(new ArrayList<>());
        } else {
            List<Params> list = paramsService.selectParamsList(params);
            return getDataTable(list);
        }
    }

    @PostMapping("/saveParams")
    @ResponseBody
    public AjaxResult saveParams(Params params) {
        paramsService.insertParams(params);
        return AjaxResult.success(params);
    }

    /**
     * 导出参数数据集列表
     */
    @RequiresPermissions("parameter:parameter:export")
    @Log(title = "参数数据集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Params params) {
        List<Params> list = paramsService.selectParamsList(params);
        ExcelUtil<Params> util = new ExcelUtil<Params>(Params.class);
        return util.exportExcel(list, "参数数据集数据");
    }

    /**
     * 新增参数数据集
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存参数数据集
     */
    //@RequiresPermissions("parameter:parameter:add")
    @Log(title = "参数数据集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Params params) {
        boolean flag = this.paramsService.check(params);
        if (flag) return AjaxResult.warn("数据集重复");
        return toAjax(paramsService.insertParams(params));
    }

    /**
     * 修改参数数据集
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Params params = paramsService.selectParamsById(id);
        mmap.put("params", params);
        return prefix + "/edit";
    }

    /**
     * 修改保存参数数据集
     */
    @RequiresPermissions("parameter:parameter:edit")
    @Log(title = "参数数据集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Params params) {
        return toAjax(paramsService.updateParams(params));
    }

    /**
     * 删除参数数据集
     */
    @RequiresPermissions("parameter:parameter:remove")
    @Log(title = "参数数据集", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(paramsService.deleteParamsByIds(ids));
    }
}
